<template>
  <div class="agent-tree-container">
    <el-card class="box-card">
      <template #header>
        <div class="card-header">
          <span>代理层级结构</span>
          <el-button type="primary" @click="handleRefresh">刷新数据</el-button>
        </div>
      </template>
      
      <div v-loading="loading">
        <el-empty v-if="treeData.length === 0" description="暂无数据" />
        
        <el-tree
          v-else
          :data="treeData"
          default-expand-all
          node-key="id"
          :props="defaultProps"
        >
          <template #default="{ data }">
            <div class="custom-tree-node">
              <div>
                <span>{{ data.name }}</span>
                <el-tag size="small" :type="getTagType(data.level)" class="ml-10">
                  {{ data.levelName }}
                </el-tag>
              </div>
              <div class="node-info">
                <div>佣金率: {{ data.commission_rate }}%</div>
                <div>余额: ¥{{ data.balance }}</div>
                <div>
                  <el-tag size="small" :type="data.status === 1 ? 'success' : 'danger'">
                    {{ data.status === 1 ? '正常' : '禁用' }}
                  </el-tag>
                </div>
                <el-button 
                  type="primary" 
                  size="small" 
                  @click.stop="handleEdit(data.id)"
                  class="ml-10"
                >
                  编辑
                </el-button>
              </div>
            </div>
          </template>
        </el-tree>
      </div>
    </el-card>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { getAgentTree } from '@/api/agents'

const router = useRouter()
const loading = ref(false)
const treeData = ref([])

const defaultProps = {
  children: 'children',
  label: 'name'
}

// 获取代理树形结构
const getTree = async () => {
  loading.value = true
  try {
    const res = await getAgentTree()
    if (res.code === 0) {
      treeData.value = res.data || []
    } else {
      ElMessage.error(res.message || '获取代理层级结构失败')
    }
  } catch (error) {
    console.error('获取代理层级结构失败', error)
    ElMessage.error('获取代理层级结构失败')
  } finally {
    loading.value = false
  }
}

// 获取标签类型
const getTagType = (level) => {
  const types = {
    1: 'info', // 普通代理
    2: 'success', // 高级代理
    3: 'warning', // 区域代理
    4: 'danger'  // 总代理
  }
  return types[level] || 'info'
}

// 刷新数据
const handleRefresh = () => {
  getTree()
}

// 编辑代理
const handleEdit = (id) => {
  router.push(`/agent/edit/${id}`)
}

onMounted(() => {
  getTree()
})
</script>

<style scoped>
.agent-tree-container {
  padding: 20px;
}
.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.custom-tree-node {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding-right: 8px;
}
.node-info {
  display: flex;
  align-items: center;
  gap: 15px;
}
.ml-10 {
  margin-left: 10px;
}
</style> 